package cn.lzj.controller.business;


import cn.lzj.Utils.GetImgUtils;
import cn.lzj.pojo.ProductBase;
import cn.lzj.pojo.ProductType;
import cn.lzj.pojo.User;
import cn.lzj.pojo.productGenerator;
import cn.lzj.service.User.ProductService;
import cn.lzj.service.User.ProductTypeService;
import cn.lzj.service.base.IndexService;
import cn.lzj.service.business.BusinessService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@Controller
@RequestMapping("business")
public class BusinessController {

    @Resource
    private BusinessService businessService;

    @Resource
    private IndexService indexService;

    @Resource
    private ProductService productService;

    @Resource
    private ProductTypeService productTypeService;


    @PostMapping("/addProductGenerator")
    public String addProductGenerator(){
        productGenerator product = new productGenerator();
        //设置product属性
        boolean success = businessService.addProduct(product);
        if(success){
            return "success";
        }else {
            return "error";
        }
    }


    @GetMapping("/delProduct/{proId}")
    public String delProduct(@PathVariable("proId") Integer proId,Model model,HttpServletRequest request){

        productGenerator productGenerator = businessService.selectProductGeneratorById(proId);
        businessService.delProduct(productGenerator);
        return GoodList(model,request);
    }

    @GetMapping("/selectProduct")
    public String selectProduct(HttpServletRequest request, Model model){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        List<productGenerator> productGeneratorList = businessService.selectProductByUserid(user);
        model.addAttribute("productGeneratorList",productGeneratorList);
        return "index";
    }


    /**
     * addGoods只是视图跳转而已
     * @param model
     * @return
     */
    @GetMapping("/addGoods")
    public String addGoods(Model model){
        List<ProductType> productTypeList = indexService.selectProductType();
        model.addAttribute("productTypeList",productTypeList);
        return "addGoods";
    }

    @PostMapping("/addProduct")
    private String addProduct(HttpServletRequest request,
                              @RequestParam("myFile") MultipartFile myFile,
                              String productName,
                              Integer productCount,
                              String productTypeName,
                              Double productPrice,
                              Model model
                              ){
        HttpSession session = request.getSession();
        String path = "D:\\myImg";

        String img = GetImgUtils.getImg(myFile, path);


        User user = (User) session.getAttribute("user");
        ProductType type = productTypeService.selectProductByName(productTypeName);

        ProductBase productBase = new ProductBase();
        productBase.setProductName(productName);
        productBase.setProductCount(productCount);
        productBase.setProductPrice(productPrice);
        productBase.setSellNum(0);
        productBase.setProductImg(img);
        productBase.setUser(user);
        productBase.setType(type);

        productService.addProduct(productBase);
        return GoodList(model,request);
    }


    @GetMapping("/GoodList")
    public String GoodList(Model model,HttpServletRequest request){
        HttpSession session = request.getSession();
        System.out.println("GoodList session.getId() = " + session.getId());


        User user = ((User) session.getAttribute("user"));
        List<ProductBase> productBaseList = productService.selectProductByUserId(user);
        model.addAttribute("productBaseList",productBaseList);

        return "GoodList";
    }

}
